package com.zbkj.crmeb.merchant.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crmeb.core.common.CommonResult;
import com.github.pagehelper.PageInfo;
import com.zbkj.crmeb.chant.dto.ChannelDiscountDTO;
import com.zbkj.crmeb.chant.model.ChannelDiscount;
import com.zbkj.crmeb.chant.request.ChannelDiscountRequest;
import com.zbkj.crmeb.chant.service.ChannelDiscountService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 满减送功能
 * @author daigb
 * @date:  2023-01-06 16:28
 */
@RestController
@RequestMapping("api/admin/mer/channel/discount")
@Api(tags = "活动--满减送")
public class MerChannelDiscountController extends AdminBaseController {
    @Autowired
    ChannelDiscountService channelDiscountService;

    /**
     * 列表
     * @Description:  
     * @author daigb 
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "列表")
    @GetMapping("/list")
    public CommonResult<List<ChannelDiscountDTO>> listEntity(ChannelDiscountRequest channelDiscountRequest, HttpServletRequest request) {
        return CommonResult.success(channelDiscountService.listEntity(getMerId(request), channelDiscountRequest));
    }
    
    /**
     * 分页
     * @Description:  
     * @author daigb 
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "分页")
    @GetMapping("/page")
    public CommonResult<PageInfo<ChannelDiscountDTO>> pageEntity(ChannelDiscountRequest channelDiscountRequest, HttpServletRequest request) {
    	return CommonResult.success(channelDiscountService.pageEntity(getMerId(request), channelDiscountRequest));
    }
    
    /**
     * 新增或修改
     * @Description:  
     * @author daigb 
     * @param @param channelDiscount
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "新增或修改")
    @PostMapping
    public CommonResult<Object> save(@RequestBody ChannelDiscountDTO channelDiscountDTO, HttpServletRequest request) {
    	if(null != channelDiscountDTO.getStartTime() && null != channelDiscountDTO.getEndTime()) {
    		if(1 == channelDiscountDTO.getStartTime().compareTo(channelDiscountDTO.getEndTime())) {
    			return CommonResult.validateFailed("开始时间不能小于结束时间");
    		}
    	}
    	
        return CommonResult.success(channelDiscountService.saveEntity(getMerId(request), channelDiscountDTO));
    }
    
    /**
     * 启用或禁用
     * @Description:  
     * @author daigb 
     * @param @param id
     * @param @param isShow
     * @param @param request
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @PutMapping("/is-enabled/{id}/update/{status}")
    @ApiOperation("禁用或启用")
    public CommonResult<Object> enabled(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id, @ApiParam(value = "启用状态 1启用 0 禁用", required = true) @PathVariable("status") Integer status, HttpServletRequest request) {
        if(1 == status) {
            return CommonResult.validateFailed("禁用后不支持启用!");
        }
    	
    	return CommonResult.success(channelDiscountService.update(Wrappers.<ChannelDiscount>lambdaUpdate()
                .eq(ChannelDiscount :: getId, id)
                .eq(ChannelDiscount :: getMerId, getMerId(request))
                .set(ChannelDiscount :: getStatus, status)
                ));
    }
    
    /**
     * 根据id删除
     * @Description:  
     * @author daigb 
     * @param @param id
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/{id}")
    public CommonResult<Object> del(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id, HttpServletRequest request) {
        return CommonResult.success(channelDiscountService.delEntity(getMerId(request), id));
    }
    
    /**
     * 根据id查询
     * @Description:  
     * @author daigb 
     * @param @param id
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public CommonResult<Object> get(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id, HttpServletRequest request) {
    	return CommonResult.success(channelDiscountService.getEntity(getMerId(request), id));
    }
    
}
